﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Sustainalytics.Entities;

namespace Sustainalytics.FTPReport.BusinessLogic.Tests
{
    [TestClass]
    public class FtpReportsTests
    {
        ReportsFtpManagement reportManagement = null;

        //Sustainalytics Toronto Account
        string guidSustainalyticsToronto = "528ea769-2d17-e411-92ce-6c3be5be9f98";

        [TestInitialize]
        public void Initialize()
        {
             reportManagement = new ReportsFtpManagement();
        }


        [TestMethod()]
        public void CreateReportFtp_Test_New_FTPReport_ForAccount()
        {
            var reportId = Guid.NewGuid();

            //Sustainalytics Toronto Account
            Guid reportAccountId = new Guid(guidSustainalyticsToronto);

            string userName = "RoxanaUser";
            string userPass = "Test";
            string serverAddress = "localhost";

            //FTP and FTP/SSL Server Port
            int serverPort = 21;      

            ReportFTPDto newReportDto = new ReportFTPDto()
            {
                 Id = reportId,
                 AccountId = reportAccountId,
                 Username = userName,
                 Password = userPass,
                 ServerAddress = serverAddress,
                 ServerPort = serverPort,
                 FtpServerType = FtpServerTypeEnum.Ftp
            };           

            reportManagement.CreateReportFtp(newReportDto);
            ReportFTP persistedFTPReport =  reportManagement.LoadReportFtp(reportId);

            Assert.IsNotNull(persistedFTPReport);
            Assert.IsTrue(persistedFTPReport.Id == reportId);
            Assert.IsTrue(persistedFTPReport.AccountId == reportAccountId);

            Assert.IsTrue(persistedFTPReport.Username == userName);
            Assert.IsTrue(persistedFTPReport.Password == userPass);
            Assert.IsTrue(persistedFTPReport.ServerAddress == serverAddress);
            Assert.IsTrue(persistedFTPReport.ServerPort == serverPort);
        }

        [TestMethod()]
        public void CreateReportFtp_Test_Delete_FTP_ReportByAccount()
        {
            var reportId = Guid.NewGuid();

            //Sustainalytics Toronto Account
            Guid reportAccountId = new Guid(guidSustainalyticsToronto);

            string userName = "RoxanaUser";
            string userPass = "Test";
            string serverAddress = "localhost";

            //FTP and FTP/SSL Server Port
            int serverPort = 21;

            ReportFTPDto newReportDto = new ReportFTPDto()
            {
                Id = reportId,
                AccountId = reportAccountId,
                Username = userName,
                Password = userPass,
                ServerAddress = serverAddress,
                ServerPort = serverPort,
                FtpServerType = FtpServerTypeEnum.Ftp
            };

            reportManagement.CreateReportFtp(newReportDto);
            ReportFTP persistedFTPReport = reportManagement.LoadReportFtp(reportId);

            Assert.IsNotNull(persistedFTPReport);
            Assert.IsTrue(persistedFTPReport.Id == reportId);
            Assert.IsTrue(persistedFTPReport.AccountId == reportAccountId);

            Assert.IsTrue(persistedFTPReport.Username == userName);
            Assert.IsTrue(persistedFTPReport.Password == userPass);
            Assert.IsTrue(persistedFTPReport.ServerAddress == serverAddress);
            Assert.IsTrue(persistedFTPReport.ServerPort == serverPort);

            reportManagement.DeleteReportFtpForAccount(reportAccountId);
            persistedFTPReport = reportManagement.LoadReportFtp(reportId);
            Assert.IsNull(persistedFTPReport);
        }

        [TestMethod()]
        public void CreateReportFtp_Test_Delete_FTPSSH_ReportByAccount()
        {
            var reportId = Guid.NewGuid();

            //Sustainalytics Toronto Account
            Guid reportAccountId = new Guid(guidSustainalyticsToronto);

            string userName = "RoxanaUser";
            string userPass = "Test";
            string serverAddress = "localhost";
            
            //SFTP Server Port
            int serverPort = 25;

            ReportFTPDto newReportDto = new ReportFTPDto()
            {
                Id = reportId,
                AccountId = reportAccountId,
                Username = userName,
                Password = userPass,
                ServerAddress = serverAddress,
                ServerPort = serverPort,
                //FtpServerType = FtpServerTypeEnum.Ftp
                FtpServerType = FtpServerTypeEnum.FtpSSH
              //   FtpServerType = FtpServerTypeEnum.FtpSSL
            };

            reportManagement.CreateReportFtp(newReportDto);
            ReportFTP persistedFTPReport = reportManagement.LoadReportFtp(reportId);

            Assert.IsNotNull(persistedFTPReport);
            Assert.IsTrue(persistedFTPReport.Id == reportId);
            Assert.IsTrue(persistedFTPReport.AccountId == reportAccountId);

            Assert.IsTrue(persistedFTPReport.Username == userName);
            Assert.IsTrue(persistedFTPReport.Password == userPass);
            Assert.IsTrue(persistedFTPReport.ServerAddress == serverAddress);
            Assert.IsTrue(persistedFTPReport.ServerPort == serverPort);

            reportManagement.DeleteReportFtpForAccount(reportAccountId);
            persistedFTPReport = reportManagement.LoadReportFtp(reportId);
            Assert.IsNull(persistedFTPReport);
        }

        [TestMethod()]
        public void CreateReportFtp_Test_Delete_FTPSSL_ReportByAccount()
        {
            var reportId = Guid.NewGuid();

            //Sustainalytics Toronto Account
            Guid reportAccountId = new Guid(guidSustainalyticsToronto);

            string userName = "AnaUser";
            string userPass = "AnaTest";
            string serverAddress = "localhost";

            //FTP and FTP/SSL Server Port
             int serverPort = 21;

            ReportFTPDto newReportDto = new ReportFTPDto()
            {
                Id = reportId,
                AccountId = reportAccountId,
                Username = userName,
                Password = userPass,
                ServerAddress = serverAddress,
                ServerPort = serverPort,
                 FtpServerType = FtpServerTypeEnum.FtpSSL
            };

            reportManagement.CreateReportFtp(newReportDto);
            ReportFTP persistedFTPReport = reportManagement.LoadReportFtp(reportId);

            Assert.IsNotNull(persistedFTPReport);
            Assert.IsTrue(persistedFTPReport.Id == reportId);
            Assert.IsTrue(persistedFTPReport.AccountId == reportAccountId);

            Assert.IsTrue(persistedFTPReport.Username == userName);
            Assert.IsTrue(persistedFTPReport.Password == userPass);
            Assert.IsTrue(persistedFTPReport.ServerAddress == serverAddress);
            Assert.IsTrue(persistedFTPReport.ServerPort == serverPort);

            reportManagement.DeleteReportFtpForAccount(reportAccountId);
            persistedFTPReport = reportManagement.LoadReportFtp(reportId);
            Assert.IsNull(persistedFTPReport);
        }

        [TestMethod()]
        public void CreateReportFtp_Test_DeleteFtpReport_ByAccountId_and_FtpReportId()
        {
            var reportId = Guid.NewGuid();

            //Sustainalytics Toronto Account
            Guid reportAccountId = new Guid(guidSustainalyticsToronto);

            string userName = "roxana.test@sustainalytics.com";
            string userPass = "Password";
            string serverAddress = "localhost";
            int serverPort = 12345;

            ReportFTPDto newReportDto = new ReportFTPDto()
            {
                Id = reportId,
                AccountId = reportAccountId,
                Username = userName,
                Password = userPass,
                ServerAddress = serverAddress,
                ServerPort = serverPort,
                FtpServerType = FtpServerTypeEnum.Ftp
            };

            reportManagement.CreateReportFtp(newReportDto);
            ReportFTP persistedFTPReport = reportManagement.LoadReportFtpForAccountId(reportAccountId);

            Assert.IsNotNull(persistedFTPReport);
            Assert.IsTrue(persistedFTPReport.Id == reportId);
            Assert.IsTrue(persistedFTPReport.AccountId == reportAccountId);

            Assert.IsTrue(persistedFTPReport.Username == userName);
            Assert.IsTrue(persistedFTPReport.Password == userPass);
            Assert.IsTrue(persistedFTPReport.ServerAddress == serverAddress);
            Assert.IsTrue(persistedFTPReport.ServerPort == serverPort);

            reportManagement.DeleteReportFtpForAccountAndReportId(reportAccountId, reportId);
            persistedFTPReport = reportManagement.LoadReportFtp(reportId);
            Assert.IsNull(persistedFTPReport);
        }


        [TestMethod()]
        public void CreateReportFtp_Test_UpdateFtpReport()
        {
            var reportId = Guid.NewGuid();

            //Sustainalytics Toronto Account
            Guid reportAccountId = new Guid(guidSustainalyticsToronto);

            string userName = "roxana.test@sustainalytics.com";
            string userPass = "Password";
            string serverAddress = "localhost";
            int serverPort = 12345;

            ReportFTPDto newReportDto = new ReportFTPDto()
            {
                Id = reportId,
                AccountId = reportAccountId,
                Username = userName,
                Password = userPass,
                ServerAddress = serverAddress,
                ServerPort = serverPort
            };

            reportManagement.CreateReportFtp(newReportDto);
            ReportFTP persistedFTPReport = reportManagement.LoadReportFtpForAccountId(reportAccountId);

            Assert.IsNotNull(persistedFTPReport);
            Assert.IsTrue(persistedFTPReport.Id == reportId);
            Assert.IsTrue(persistedFTPReport.AccountId == reportAccountId);

            Assert.IsTrue(persistedFTPReport.Username == userName);
            Assert.IsTrue(persistedFTPReport.Password == userPass);
            Assert.IsTrue(persistedFTPReport.ServerAddress == serverAddress);
            Assert.IsTrue(persistedFTPReport.ServerPort == serverPort);

            

            string updatedUserName = "roxana.gherg@sustainalytics.com";
            string updatedUserPass = "Password_Updated";
            string updatedServerAddress = "localhostTest";
            int updatedServerPort = 6789;

            newReportDto.Username = updatedUserName;
            newReportDto.Password = updatedUserPass;
            newReportDto.ServerAddress = updatedServerAddress;
            newReportDto.ServerPort = updatedServerPort;

            reportManagement.UpdateReportFtp(newReportDto);

            persistedFTPReport = reportManagement.LoadReportFtp(reportId);

            Assert.IsTrue(persistedFTPReport.Username.Equals(updatedUserName));
            Assert.IsTrue(persistedFTPReport.Password.Equals(updatedUserPass));
            Assert.IsTrue(persistedFTPReport.ServerAddress.Equals(updatedServerAddress));
            Assert.IsTrue(persistedFTPReport.ServerPort.Equals(updatedServerPort));

            reportManagement.DeleteReportFtpForAccountAndReportId(reportAccountId, reportId);
            persistedFTPReport = reportManagement.LoadReportFtp(reportId);

            Assert.IsNull(persistedFTPReport);
        }

        #region FTPClientReport

        [TestMethod()]
        public void AddFtpClientReport_Test()
        {
            var entityId = Guid.NewGuid();
            var clientReportId = Guid.NewGuid();

            //Sustainalytics Toronto Account
            Guid reportAccountId = new Guid(guidSustainalyticsToronto);
                      
            var frequency = FrequencyEnum.Daily;

            FtpClientReportDto newClientFtpReportDto = new FtpClientReportDto()
            {
                Id = entityId,
                ReportId = clientReportId,
                AccountId = reportAccountId,
                Frequency = frequency,
                //Name = "Report Rox 1",
                
            };

            reportManagement.AddFtpClientReport(newClientFtpReportDto);
            FtpClientReport persistedClientFTPReport = reportManagement.LoadFtpClientReport(reportAccountId, clientReportId);

            Assert.IsNotNull(persistedClientFTPReport);
            Assert.IsTrue(persistedClientFTPReport.Id == entityId);
            Assert.IsTrue(persistedClientFTPReport.AccountId == reportAccountId);
            Assert.IsTrue(persistedClientFTPReport.ReportId == clientReportId);

            Assert.IsTrue(persistedClientFTPReport.Frequency == frequency);

            reportManagement.DeleteFtpClientReport(reportAccountId, clientReportId);

            persistedClientFTPReport = reportManagement.LoadFtpClientReport(reportAccountId, clientReportId);
            Assert.IsNull(persistedClientFTPReport);
        }

        [TestMethod()]
        public void ModifyFtpClientReport_Test()
        {
            var entityId = Guid.NewGuid();
            var clientReportId = Guid.NewGuid();

            //Sustainalytics Toronto Account
            Guid reportAccountId = new Guid(guidSustainalyticsToronto);

            var frequency = FrequencyEnum.Monthly;

            FtpClientReportDto newClientFtpReportDto = new FtpClientReportDto()
            {
                Id = entityId,
                ReportId = clientReportId,
                AccountId = reportAccountId,
                Frequency = (FrequencyEnum)frequency,
                //Name = "Report Rox 1",
                
            };
                       
            reportManagement.AddFtpClientReport(newClientFtpReportDto);
            FtpClientReport persistedClientFTPReport = reportManagement.LoadFtpClientReport(reportAccountId, clientReportId);

            Assert.IsNotNull(persistedClientFTPReport);
            Assert.IsTrue(persistedClientFTPReport.Id == entityId);
            Assert.IsTrue(persistedClientFTPReport.AccountId == reportAccountId);
            Assert.IsTrue(persistedClientFTPReport.ReportId == clientReportId);

            Assert.IsTrue(persistedClientFTPReport.Frequency == frequency);

            var updatedFrequency = FrequencyEnum.Quarterly;
            var updatedClientReportId = Guid.NewGuid();
            
            newClientFtpReportDto.Frequency = updatedFrequency;
            newClientFtpReportDto.ReportId = updatedClientReportId;

            reportManagement.ModifyFtpClientReport(newClientFtpReportDto);

            FtpClientReport updatedClientFTPReport = reportManagement.LoadFtpClientReport(reportAccountId, updatedClientReportId);
            Assert.IsNotNull(updatedClientFTPReport);
            Assert.IsTrue(updatedClientFTPReport.Id == entityId);
            Assert.IsTrue(updatedClientFTPReport.AccountId == reportAccountId);
            Assert.IsTrue(updatedClientFTPReport.ReportId == updatedClientReportId);

            reportManagement.DeleteFtpClientReport(reportAccountId, updatedClientReportId);

            persistedClientFTPReport = reportManagement.LoadFtpClientReport(reportAccountId, clientReportId);
            Assert.IsNull(persistedClientFTPReport);
        }

        #endregion


    }
}
